//
//  DDWorkDetailViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/17.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDWorkDetailViewController.h"
#import "DDShareView.h"
#import "WXApi.h"
#import <WeiboSDK.h>
#import "DDCommonCell.h"
#import "DDJobDetailCell.h"
#import "DDBossDetailCell.h"
#import "DDTeamDescriptionCell.h"
#import "DDJobDescriptionCell.h"
#import "DDCompanyInfoCell.h"
#import "DDJobsService.h"
#import "DDChatMessageViewController.h"
#import "DDBossJobsListViewController.h"
#import "DDCommunicatedStore.h"
#import <MessageUI/MessageUI.h>
#import "DDShareManager.h"
#import "DDCommonDataService.h"
#import "DDCommonDataModel.h"


#define kJobDetailCellHeight          @125
#define kBossDetailCellHeight         @88
#define kTeamDescriptionCellHeight    @145
#define kJobDescriptionCellHeight     @186
#define kCompanyInfoCellHeight        @170

@interface DDWorkDetailViewController () <DDShareViewDelegate, MFMessageComposeViewControllerDelegate>


@property (strong, nonatomic) DDJobsService *jobService;

@property (strong, nonatomic) DDCommonDataService *commonService;

@property (strong, nonatomic) DDShareView *shareView;
@property (weak, nonatomic) IBOutlet UIView *tooView;
@property (weak, nonatomic) IBOutlet UIButton *interestingButton;
@property (weak, nonatomic) IBOutlet UIButton *chatButton;
@property (weak, nonatomic) IBOutlet UIView *toolBarView;

@property (strong, nonatomic) NSMutableArray *companyInfo;


@property (strong, nonatomic) NSMutableArray *rowHeightArray;
@end

@implementation DDWorkDetailViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.rowHeightArray = [[NSMutableArray alloc] init];
        self.jobService = [[DDJobsService alloc] init];
        self.commonService = [[DDCommonDataService alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setUpView];
    [self setUpData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
}

- (DDShareView *)shareView
{
    if (!_shareView) {
        _shareView = (DDShareView *)[[NSBundle mainBundle] loadNibNamed:@"DDShareView" owner:self options:nil].firstObject;
        
        [_shareView.cancelButton addTarget:self action:@selector(shareCancelButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
        _shareView.delegate = self;
        
        WEAKSELF
        [_shareView bk_whenTapped:^{
            [weakSelf.shareView hide];
        }];
    }
    
    return _shareView;
}

- (void)setUpView
{
    self.title = self.jobDetail.jobtitle;
    
//    [self addRightButtonItemWithImage:[UIImage imageNamed:@"common_nav_share"] action:@selector(cattleReportButtonClick)];
    [self addNavigationRightBar];
    
    [self.view bringSubviewToFront:self.tooView];
    self.tooView.layer.borderWidth = 1;
    self.tooView.layer.borderColor = COLOR_BORDER_GRAY.CGColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(6, 0, 0, 0);
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DDJobDetailCell" bundle:nil] forCellReuseIdentifier:@"DDJobDetailCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DDBossDetailCell" bundle:nil] forCellReuseIdentifier:@"DDBossDetailCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DDTeamDescriptionCell" bundle:nil] forCellReuseIdentifier:@"DDTeamDescriptionCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DDJobDescriptionCell" bundle:nil] forCellReuseIdentifier:@"DDJobDescriptionCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DDCompanyInfoCell" bundle:nil] forCellReuseIdentifier:@"DDCompanyInfoCellID"];
    
    
    
    
    if ([DDLoginUserManager isLogin]&&self.BossClickByChat == NO) {
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = COLOR_BORDER_GRAY;
        [self.view addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_interestingButton.mas_right);
            make.centerX.mas_equalTo(self.view);
            make.centerY.mas_equalTo(_interestingButton);
            make.size.mas_equalTo(CGSizeMake(1, 40));
            
        }];
        
        self.toolBarView.hidden = false;
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.height.mas_equalTo(self.view.mas_height).mas_offset(-50);
        }];
        
    } else {
        
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.height.mas_equalTo(self.view);
        }];
        
        self.toolBarView.hidden = true;
        
    }
    
}

- (void)addNavigationRightBar
{
    /**
     未登录时候，不显示举报按钮
     */
    if ([DDLoginUserManager isLogin]){
    UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 66, 44)];
    UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeCustom];
    reportButton.frame= CGRectMake(32, 1, 42, 42);
    [reportButton setImage:[UIImage imageNamed:@"common_nav_report"] forState:UIControlStateNormal];
    [reportButton addTarget:self action:@selector(cattleReportButtonClick)forControlEvents:UIControlEventTouchDown];
    [rightBarView addSubview:reportButton];
    rightBarView.backgroundColor=[UIColor clearColor];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];
    self.navigationItem.rightBarButtonItem = rightBtn;
        //    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //    shareButton.frame= CGRectMake(38, 3, 28, 28);
        //    [shareButton setImage:[UIImage imageNamed:@"common_nav_share"] forState:UIControlStateNormal];
        //    [shareButton addTarget:self action:@selector(shareButtonClick)forControlEvents:UIControlEventTouchDown];
        //    [rightBarView addSubview:shareButton];
    }
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    WEAKSELF
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 50, 0));
    }];
}

- (void)setUpData
{
    self.rowHeightArray = [NSMutableArray arrayWithArray:@[kJobDetailCellHeight,kBossDetailCellHeight,kTeamDescriptionCellHeight,kJobDescriptionCellHeight,kCompanyInfoCellHeight]];
    
    [self queryJobDetailInfo];
    
    WEAKSELF
////    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
//    dispatch_get_main_queue(), ^{
//        [weakSelf queryInterestStatus];
////    });
    if ([DDLoginUserManager isLogin]) {
        [weakSelf queryInterestStatus];
    }
}

#pragma mark - evnets

- (void)onBossPublishedJobsClick
{
    DDBossJobsListViewController *vc = [[DDBossJobsListViewController alloc] init];
    vc.jobDetail = self.jobDetail;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)actionInterest:(id)sender
{
    [self markInterstStatus];
}

- (IBAction)actionChat:(id)sender
{
    //employer ,jobfinder
    NSString *typeString, *targetId;
    if ([DDClientInfoManager clientType] == DDClientTypeBoss) {
        typeString = @"jobfinder";
    } else {
        typeString = @"employer";
    }
    
    if (self.jobDetail) {
        targetId = [NSString stringWithFormat:@"%@_%@",self.jobDetail.userid,typeString];
    } else {
        targetId = @"3";
    }
    
    DDChatMessageViewController *vc = [[DDChatMessageViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:targetId];
    vc.title = self.jobDetail.employername;
    /// 如果有jobdetail，传入聊天界面进行缓存
    if (self.jobDetail) {
        
        vc.jobid = self.jobDetail.jobid;
    }
    
//    ///	缓存jobdetail
//    [[DDCommunicatedStore sharedStore] storeCommunicatedBoss:self.jobDetail];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)shareButtonClick
{
    CGRect originalBound = self.shareView.actionView.frame;
    originalBound.origin.y +=210;
    self.shareView.actionView.frame = originalBound;
    
    if (!self.shareView.superview) {
        
        [UIView animateWithDuration:0.3 animations:^{
            [[UIApplication sharedApplication].keyWindow addSubview:self.shareView];
            
            CGRect newBounds = self.shareView.actionView.frame;
            newBounds.origin.y -= 210;
            self.shareView.actionView.frame = newBounds;
        }];
    }
}

- (void)shareCancelButtonOnClick:(UIButton *)button
{
    [self.shareView hide];
}

- (void)cattleReportButtonClick
{
 
    WEAKSELF
    [self.commonService queryAllComplaintsForBoss:^(NSArray *complaints) {
       
        NSMutableArray *reportTitleArray = [@[] mutableCopy];
        NSMutableArray *reportIDArray = [@[] mutableCopy];
        
        for (DDCommonDataModel *reportContent in complaints) {

            [reportTitleArray addObject:reportContent.complaint_text];
            [reportIDArray addObject:reportContent.complaint_id];
        }
        
        [weakSelf showLDActionSheetView:@"举报对方" labHeight:nil labsTitleArr:reportTitleArray selClosure:^(NSInteger index) {
            
            [weakSelf reportBossWithID:reportIDArray[index]];
            
        }];
    }];
}

- (void)reportBossWithID:(NSString *)ID
{
    WEAKSELF
    if (!self.jobDetail.userid) {
        [self showToastOnCenter:@"用户Id为空"];
        return;
    }
    [self.jobService reportBossWithBossId:self.jobDetail.userid reportId:ID successBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            
            [[LDShowHUB shareInstance] showToastNoti:@"举报成功"];
//            [weakSelf showSuccessWithText:@"感谢您的举报，我们将尽快处理！"];
        } else {
            [[LDShowHUB shareInstance] showToastNoti:@"举报失败"];
//            [weakSelf responseUnValidate:responseObject];
        }

    } failBlock:^(id responseObject, NSError *error) {
        [[LDShowHUB shareInstance] showToastNoti:@"举报失败"];
//        [weakSelf requestFaild];
    }];
}

#pragma mark
#pragma mark - Private Method

- (void)queryJobDetailInfo
{
    WEAKSELF
    
    if ([DDLoginUserManager isLogin]) {
        
        [self.jobService queryLoginJobsDetailWithJobId:self.jobDetail.jobid successBlock:^(id responseObject) {
            
            if ([weakSelf isValidateResponseData:responseObject]) {
                DDJobsDetail *detailInfo = [DDJobsDetail yy_modelWithJSON:responseObject[@"data"]];
                weakSelf.jobDetail = detailInfo;
                [weakSelf updateView];
                
            } else {
                [weakSelf responseUnValidate:responseObject];
            }
        } failBlock:^(id responseObject, NSError *error) {
            [weakSelf requestFaild];
        }];
        
    } else {
        
        [self.jobService queryNoLoginJobsDetailWithJobId:self.jobDetail.jobid successBlock:^(id responseObject) {
            
            if ([weakSelf isValidateResponseData:responseObject]) {
                
                DDJobsDetail *detailInfo = [DDJobsDetail yy_modelWithJSON:responseObject[@"data"]];
                weakSelf.jobDetail = detailInfo;
                [weakSelf updateView];
                
            } else {
                
                [weakSelf responseUnValidate:responseObject];
            }
            
        } failBlock:^(id responseObject, NSError *error) {
            
            [weakSelf requestFaild];
        }];
        
    }
    
}

- (void)queryInterestStatus
{
    WEAKSELF
    
    [self.jobService queryInterestedJobWithJobId:self.jobDetail.jobid successBlock:^(id responseObject) {

        if ([weakSelf isValidateResponseData:responseObject]) {
            NSString *status = [NSString stringWithFormat:@"%@",responseObject[@"data"]];

            self.interestingButton.selected = [status isEqualToString:@"1"];

        } else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

- (void)markInterstStatus
{
    WEAKSELF
    
    [self.jobService markInterestingJobWithJobId:self.jobDetail.jobid successBlock:^(id responseObject) {
      
        if ([weakSelf isValidateResponseData:responseObject]) {
            NSString *status = [NSString stringWithFormat:@"%@",responseObject[@"data"]];
            
            self.interestingButton.selected = [status isEqualToString:@"1"];
            
            if (self.interestingButton.selected == YES) {
                
                [[LDShowHUB shareInstance]showToastSucc:@"关注成功"];
            }else{
          
                [[LDShowHUB shareInstance]showToastSucc:@"取消关注"];
            }
        } else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

/**
 *  刷新View数据
 */
- (void)updateView
{
    self.title = self.jobDetail.jobtitle;
    // 工作详情
    DDJobDetailCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    [cell updateJobDetailCellWithInfo:self.jobDetail];
    
    // Boss详情
    DDBossDetailCell *bossCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
//    [bossCell.avatarImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kImagePre,self.jobDetail.logo]] placeholderImage:[UIImage imageNamed:@"common_avatar"]];
    
    [bossCell.avatarImage sd_setImageWithURL:[NSURL URLWithString:[DDAPIGenerate imageURLWithPath:self.jobDetail.logo]] placeholderImage:[UIImage imageNamed:@"common_avatar"]];
    bossCell.nameLabel.text = self.jobDetail.employername;
    bossCell.personInfoLabel.text = [NSString stringWithFormat:@"%@ | %@ | %@",!self.jobDetail.employername?@"姓名":self.jobDetail.employername,!self.jobDetail.companyshortname?@"公司":self.jobDetail.companyshortname,!self.jobDetail.position?@"职位":self.jobDetail.position];
    bossCell.verifyImageView.hidden = ![self.jobDetail.is_employer_verified isEqualToString:@"1"];
    [bossCell.publishedjobsButton setTitle:[NSString stringWithFormat:@"  共%@个职位  ",self.jobDetail.jobsnum] forState:UIControlStateNormal];
    [bossCell.publishedjobsButton addTarget:self action:@selector(onBossPublishedJobsClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 团队介绍
    DDTeamDescriptionCell *teamCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    
    [teamCell updateTeamDescriptionCellWithInfo:self.jobDetail];
    
    CGFloat cellHeight = 50;
    CGFloat textWidth = kScreenWidth - (15*2);//width
    
    CGFloat textHeight = [DDUtils heightWithWidth:textWidth lineSpace:10 font:[UIFont systemFontOfSize:13] text:self.jobDetail.teamintro];
    //[DDUtils heightWithWidth:textWidth font:[UIFont systemFontOfSize:12.f] text:self.jobDetail.teamintro];
    
    if (self.jobDetail.teamintro.length > 0 && self.jobDetail.teamhighlights.count < 1) {
        cellHeight += textHeight + 10;
    }
    
    if ([self.jobDetail.teamhighlights count] > 0 && [self.jobDetail.teamhighlights count] < 5) {
        
        cellHeight += 45;
    } else if ([self.jobDetail.teamhighlights count] > 4 && [self.jobDetail.teamhighlights count] < 9) {
        
        cellHeight += 85;
    } else  {
        
        cellHeight -= 9;
    }
    
    cellHeight += (10 + textHeight);
    
    if ([self.rowHeightArray count] > 3) {
        [self.rowHeightArray replaceObjectAtIndex:2 withObject:@(cellHeight)];
//        [self.tableView reloadData];
    }
    
    // 职位描述
    DDJobDescriptionCell *jobCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    [jobCell updateJobDescriptionCellWithInfo:self.jobDetail];
    
    CGFloat jobCellHeight = 50;
    CGFloat jobTextWidth = kScreenWidth - (15*2);//width
    
    CGFloat jobTextHeight = [DDUtils heightWithWidth:jobTextWidth lineSpace:10 font:[UIFont systemFontOfSize:13] text:self.jobDetail.jobdesc];
    //[DDUtils heightWithWidth:jobTextWidth font:[UIFont systemFontOfSize:12.f] text:self.jobDetail.jobdesc];
    
    jobCellHeight += jobTextHeight;
    jobCellHeight += 20;
    if ([self.rowHeightArray count] > 4) {
        [self.rowHeightArray replaceObjectAtIndex:3 withObject:@(jobCellHeight)];
//        [self.tableView reloadData];
    }

    // 公司信息
//    DDCompanyInfoCell *companyCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
    
//    companyCell.model = self.jobDetail;
//    companyCell.companyNumberLabel.text = self.jobDetail.companysize;
//    companyCell.companyUrlLabel.text = self.jobDetail.companywebsite;
//    companyCell.companyNameLabel.text = self.jobDetail.companyfullname;
//    companyCell.companyAddressLabel.text = self.jobDetail.companyaddress;
    
    [self.tableView reloadData];
}
#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.rowHeightArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.rowHeightArray[indexPath.row] floatValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (indexPath.row == 0) {
        
        cell = (DDJobDetailCell *)[tableView dequeueReusableCellWithIdentifier:@"DDJobDetailCell"];

    } else if (indexPath.row == 1) {
        

        cell = (DDBossDetailCell *)[tableView dequeueReusableCellWithIdentifier:@"DDBossDetailCell"];
        
    } else if (indexPath.row == 2) {
        

        cell = (DDTeamDescriptionCell *)[tableView dequeueReusableCellWithIdentifier:@"DDTeamDescriptionCell"];
        
    } else if (indexPath.row == 3) {
        

        cell = (DDJobDescriptionCell *)[tableView dequeueReusableCellWithIdentifier:@"DDJobDescriptionCell"];

    } else {
        

        cell = (DDCompanyInfoCell *)[tableView dequeueReusableCellWithIdentifier:@"DDCompanyInfoCellID"];
        DDCompanyInfoCell *companycell = (DDCompanyInfoCell *)cell;
        companycell.model = self.jobDetail;
        
        WEAKSELF
        companycell.tapUrlAction = ^(NSString *url) {
            
            if ([url hasPrefix:@"https"] || [url hasPrefix:@"www"]) {
            NSString *urlStr = url;
            if ([LDUtils validateUrl:url]) {
                
                if ([url hasPrefix:@"www"]) {
                    urlStr = [NSString stringWithFormat:@"http://%@",url];
                }
                
                LDBaseWebViewController *vc = [[LDBaseWebViewController alloc] init];
                vc.reqUrl = urlStr;
                
                [weakSelf.navigationController pushViewController:vc animated:true];
                
            } else {
                
                [[LDShowHUB shareInstance]showErr:@"网址无效"];
            }
        };
        
//        if (self.jobDetail != nil) {
//            
//            companycell.model = self.jobDetail;
//        }
        };
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark
#pragma mark - DDShareViewDelegate

- (void)shareViewButtonClick:(DDShareType)sharetype
{
    switch (sharetype) {
        case DDShareTypeWXTimeLine: {
            [self shareToWeiXinWithType:DDShareTypeWXTimeLine];
            
        }
            break;
            
        case DDShareTypeWeiXinSession: {
            [self shareToWeiXinWithType:DDShareTypeWeiXinSession];
        }
            break;
            
        case DDShareTypeWeiBo: {
            [self shareToWeibo];
        }
            break;
            
        case DDShareTypeSMS: {
            [self showMessageView];
        }
            break;
            
        default:
            break;
    }
}

- (void)shareToWeiXinWithType:(DDShareType)shareType
{
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = kShareTitle;
    message.description = kShareContent;
    [message setThumbImage:[UIImage imageNamed:@"res2.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = kShareURL;
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    if (shareType == DDShareTypeWeiXinSession) {
        req.scene = WXSceneSession;
    } else {
        req.scene = WXSceneTimeline;
    }

    [WXApi sendReq:req];
}

- (void)shareToWeibo
{
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare]];
    request.userInfo = @{@"ShareMessageFrom": @"DDWorkDetailViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         };
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}

- (WBMessageObject *)messageToShare
{
    WBMessageObject *message = [WBMessageObject message];
    
    
    WBWebpageObject *webpage = [WBWebpageObject object];
    webpage.objectID = @"identifier1";
    webpage.title = kShareTitle;
    webpage.description = kShareURL;
    webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_2" ofType:@"jpg"]];
    webpage.webpageUrl = kShareURL;
    message.mediaObject = webpage;
    
    
    return message;
}

- (void)showMessageView
{
    if( [MFMessageComposeViewController canSendText] ){
        
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.body = [NSString stringWithFormat:@"%@ %@",kShareTitle,kShareURL];
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES
                         completion:^{
                         }];
        
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"Invite"];//修改短信界面标题
    }else{
     
        [UIAlertView bk_alertViewWithTitle:@"提醒" message:@"设备不支持短信功能"];
    }
    
}

/**
 *  发送完短信的回调
 *
 *  @param controller
 *  @param result
 */
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    [controller dismissViewControllerAnimated:NO completion:^{
        
    }];//关键的一句   不能为YES
    
    switch (result) {
            
        case MessageComposeResultCancelled:
            
//            [self showText:@"Send Cancel"];
            break;
        case MessageComposeResultFailed:// send failed
            
            [self showToastOnCenter:@"发送失败"];
            break;
        case MessageComposeResultSent:
            
            [self showToastOnCenter:@"发送成功"];
            break;
        default:
            break;
    }
}

@end
